/*
 * Giffel, Android application
 *
 * Copyright (C) 2011  Mid Sweden University
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package se.appening;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends Activity implements OnItemClickListener {

    public static MainController controller = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (MainActivity.controller == null) {
            // MainActivity.controller = new MainController(this, new MockCringleGetter());
            MainActivity.controller = new MainController(this);
        }

        Entry[] entries = controller.getEntries();

        String[] stringList = new String[entries.length];
        for (int i = 0; i != stringList.length; i++) {
            stringList[i] = entries[i].getDistance() + "m: "
                    + entries[i].getTitle();
        }

        ArrayList<Entry> entriesList = new ArrayList<Entry>();
        for (int i = 0; i != entries.length; i++) {
            entriesList.add(entries[i]);
        }

        // Populate the list with cringle entries
        final ListView list = (ListView) findViewById(R.id.list);
        list.setOnItemClickListener(this);


        ListAdapter adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, stringList);
        list.setAdapter(adapter);

        Thread t = new RefreshThread(this);
        t.start();
    }

    private class RefreshThread extends Thread {

        private Activity context;

        public RefreshThread(Activity context) {
            this.context = context;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ListView list = (ListView) findViewById(R.id.list);
                            Entry[] entries = controller.getEntries();
                            String[] stringList = new String[entries.length];
                            for (int i = 0; i != stringList.length; i++) {
                                stringList[i] =
                                    entries[i].getDistance() + "m: " + entries[i].getTitle();
                            }
                            ListAdapter adapter = new ArrayAdapter<String>(context,
                                    android.R.layout.simple_list_item_1, stringList);
                            list.setAdapter(adapter);
                            list.postInvalidate();

                            Log.e("SDFAF", "Update");
                        }
                    });

                    Thread.sleep(10000);
                }
            } catch (Exception e) {
                Log.e("dsasd", e.toString());
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
        Intent newActivity = new Intent(this, InfoActivity.class);
        newActivity.putExtra("entry", pos);
        startActivity(newActivity);
    }
    
}